<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include "database.php";
include "global.php";


function query_type($product_id){
    $sql = "SELECT name,ratio,asset,nameunit,id FROM product_type  WHERE product_id=".$product_id;
   // echo $sql;
    $product = mysql_query($sql);

    return json_encode($product);
}
function query_product($receipt_id){
    $sql = "SELECT PT.ratio,T.id,T.receipt_id,T.product_id,P.name,PT.name Typename,T.amount,T.buyprice,T.asset   FROM transaction T, product P, product_type PT WHERE T.receipt_id=".$receipt_id." AND T.transaction_type=0 AND P.id=T.product_id AND T.product_type_id=PT.id";
   // echo $sql;
    $query = mysql_query($sql);
  //  echo $query;
    $i=0;
    while ($row = mysql_fetch_array($query)) {
        $output['type'][$i]['receipt_id']=$row['receipt_id'];
        $output['type'][$i]['product_id']=$row['product_id'];
        $output['type'][$i]['name']=$row['name'];
        $output['type'][$i]['Typename']=$row['Typename'];
        $output['type'][$i]['amount']=$row['amount'];
        $output['type'][$i]['buyprice']=$row['buyprice'];
        $output['type'][$i]['asset']=$row['asset'];
        $output['type'][$i]['ratio']=$row['ratio'];
        $output['type'][$i]['id']=$row['id'];
        $i++;
    }

echo json_encode($output);

}
function return_product($get){
    extract($get);
   // echo $return."\n".$returndata;
    $sql="SELECT * FROM transaction WHERE id=".$return;
     if(!mysql_query($sql))
        echo "Update Stock error";
    $query=mysql_query($sql);
   $result=mysql_fetch_array($query);
  
   $sql = "INSERT INTO transaction(receipt_id,product_id,transaction_type,product_type_id,asset,buyprice,amount) VALUES(".$result[receipt_id].",".$result[product_id].",1,".$result[product_type_id].",".$result[asset].",".$result[buyprice].",".$returndata.")";
    echo $sql;
    mysql_query($sql);
    $newadd = $result[ratio] * $returndata;

    $sql="SELECT ratio FROM product_type WHERE id=".$result['product_type_id'];
     if(!mysql_query($sql))
        echo "Update Stock error";
    $query=mysql_query($sql);
   $result2=mysql_fetch_array($query);
 $newadd = $result2[ratio] * $returndata;
    $sql="UPDATE product SET amount=amount+".$newadd." WHERE id=".$result[product_id];
   echo $sql;
    if(!mysql_query($sql))
        echo "Update Stock error";

}

function addStock($getItem){
    extract($getItem);
    $newadd = $ratio * $addStock;

    $sql="UPDATE product SET amount=amount+".$newadd." WHERE id=".$id;
   echo $sql;
    if(!mysql_query($sql))
        echo "Update Stock error";
    $sql="INSERT INTO stock_log(product_id,product_type_id,amount) VALUES(".$id.",".$sort.",".$addStock.")";
    if(!mysql_query($sql))
        echo "Add Stock_Log error";
}
function showAll(){
    $sql = "SELECT S.id,P.product_id,T.name,T.ratio,S.amount,T.nameunit FROM product_type T,product P,stock_log S WHERE S.product_type_id=T.id AND P.id=S.product_id ORDER BY S.product_id";
    echo $sql;
    $output = mysql_query($sql);

    return $output;
}
function delete($get){
    extract($get);
    $sumdel = $ratio * $amount;

    $sql="UPDATE product SET amount=amount-".$sumdel." WHERE product_id='".$product."'";
   echo $sql;
    if(!mysql_query($sql))
        echo "Update Product error";
    $sql="DELETE FROM stock_log WHERE id=".$id;
    echo $sql;
    if(!mysql_query($sql))
        echo "Delete Stock error";

}

?>
